﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Forms.Design
{
    /// <summary>
    /// 扩展 Marmat.Forms.DataGrid 的设计模式行为。
    /// </summary>
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class DataGridDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection verbCollection = new DesignerVerbCollection();
                verbCollection.Add(new DesignerVerb("生成绑定控件", this.CreateControls));
                //base.Verbs.Add(new DesignerVerb("检查保存方案", Me.检查保存方案));
                //base.Verbs.Add(new DesignerVerb("更新列类型", Me.更新列类型));
                return verbCollection;
            }
        }

        /// <summary>
        /// 为 System.ComponentModel.Design.DesignSurfaceManager.ActiveDesignSurfaceChanged事件提供数据。
        /// </summary>
        /// <param name="properties"></param>
        protected override void PostFilterProperties(IDictionary properties)
        {
            base.PostFilterProperties(properties);
            string[] collection = { "AllowDrop", "AutoScaleDimensions", "AutoScroll", "AutoScrollMargin", "AutoScrollMinSize", "AutoScrollOffset", "AutoScrollPosition","AutoSize", "AutoSizeMode",
                "BackColor", "BackgroundImage", "BackgroundImageLayout", "BindingContext", "ContextMenu", "ContextMenuStrip", "DataBindings", "MaximumSize", "MinimumSize", "Padding" };
            foreach (var name in collection)
            {
                properties.Remove(name);
            }
        }

        /// <summary>
        /// 允许设计器从通过 System.ComponentModel.TypeDescriptor 公开的事件集中更改或移除项。
        /// </summary>
        /// <param name="events">组件类的事件</param>
        protected override void PostFilterEvents(IDictionary events)
        {
            base.PostFilterEvents(events);

            List<string> vals = new List<string>();
            List<object> collection = new List<object>();
            vals.AddRange(new string[] { "CustomRowCellEditForEditing", "CustomRowCellEdit", "HiddenEditor", "ShowingEditor", "ShownEditor", "ValidatingEditor" });
            vals.AddRange(new string[] { "CellMerge" });
            vals.AddRange(new string[] { "KeyDown", "KeyPress", "KeyUp" });
            vals.AddRange(new string[] { "GotFocus", "LostFocus" });
            vals.AddRange(new string[] { "CustomColumnGroup", "CustomColumnSort", "EndGrouping", "EndSorting", "StartGrouping", "StartSorting" });
            vals.AddRange(new string[] { "CellValueChanged", "CellValueChanging", "ColumnWidthChanged", "FocusedColumnChanged", "FocusedRowChanged", "SelectionChanged", "TopRowChanged" });
            vals.AddRange(new string[] { "MouseDown", "MouseEnter", "MouseLeave", "MouseMove", "MouseUp", "MouseWheel" });
            vals.AddRange(new string[] { "CustomColumnDisplayText" });
            vals.AddRange(new string[] { "CalcRowHeight", "RowCellStyle", "RowStyle" });
            vals.AddRange(new string[] { "CustomDrawCell", "CustomDrawColumnHeader" });
            vals.AddRange(new string[] { "BeforeLeaveRow", "InvalidRowException", "InvalidValueException", "RowCellClick", "RowClick", "ValidateRow" });

            vals.AddRange(new string[] { "PositionChanged", "CurrentChanged", "BeforeShowContextMenu", "RowCellDoubleClick", "CellNewRowLink", "CellLinkClick", "CellButtonClick" });

            vals.AddRange(new string[] { "AfterLoad", "DataColChanging", "DataColChanged", "BeforeSaveData" });
            vals.AddRange(new string[] { "BeforeAddDataRow", "DataRowAdding", "DataRowAdded" });
            vals.AddRange(new string[] { "BeforeDeleteDataRow", "DataRowDeleting", "DataRowDeleted" });
            vals.AddRange(new string[] { "AfterMoveRow" });

            foreach (DictionaryEntry item in events)
            {
                if (!vals.Contains(item.Key))
                {
                    collection.Add(item.Key);
                }
            }

            foreach (var item in collection)
            {
                events.Remove(item);
            }
        }

        private void CreateControls(object sender, EventArgs e)
        {
            try
            {
                using (BindingFieldDesigner designer = new BindingFieldDesigner())
                {
                    IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
                    designer.CreateComponent(host, this.Component as IBindingSource);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "运行异常");
            }
        }
    }
}
